package com.chen.palmar.common.widget.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.chen.palmar.R;
import com.primb.androidlibs.utils.DisplayUtils;

/**
 * Created by Chen on 2017/3/13.
 * 功能描述：
 */

public class BarChartView extends View {
    private int width;
    private Paint paint;
    private double valueX = 0d;
    private static final int KEY_HEIGHT = 30;
    private int height;

    public BarChartView(Context context) {
        super(context);
        init();
    }

    public BarChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BarChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getWidth();
        height = getHeight();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        paint = new Paint();
        paint.setStrokeWidth(10f);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        //绘制游标
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.seek_select_icon);
        int startHeight = (height - bitmap.getHeight() - KEY_HEIGHT) / 2;
        if (((int) (width * valueX)) < bitmap.getWidth() / 2) {
            canvas.drawBitmap(bitmap, ((int) (width * valueX)), startHeight, paint);
        } else if (((int) (width * (1 - valueX))) < bitmap.getWidth() / 2) {
            canvas.drawBitmap(bitmap, width - bitmap.getWidth(), startHeight, paint);
        } else {
            canvas.drawBitmap(bitmap, ((int) (width * valueX)) - bitmap.getWidth() / 2, startHeight, paint);
        }

        //绘制底色
        paint.setColor(Color.parseColor("#D4D1D1"));
        RectF rectBg = new RectF(0, startHeight + bitmap.getHeight(), width, startHeight + bitmap.getHeight() + KEY_HEIGHT);
        canvas.drawRoundRect(rectBg, KEY_HEIGHT / 2, KEY_HEIGHT / 2, paint);
        //绘制进度
        paint.setColor(Color.parseColor("#ff5900"));
        RectF rectPro = new RectF(0, startHeight + bitmap.getHeight(), ((int) (width * valueX)), startHeight + bitmap.getHeight() + KEY_HEIGHT);
        canvas.drawRoundRect(rectPro, KEY_HEIGHT / 2, KEY_HEIGHT / 2, paint);
        //绘制文字
        paint.setColor(Color.parseColor("#666666"));
        paint.setTextSize(DisplayUtils.sp2px(26));
        paint.setTextAlign(Paint.Align.CENTER);
        String text = valueX * 100 + "%";
        Paint.FontMetrics fontMetrics = paint.getFontMetrics();
        float widthText = paint.measureText(text);
        if ((this.width * valueX) < widthText / 2) {
            canvas.drawText(text, widthText / 2, startHeight + bitmap.getHeight() + KEY_HEIGHT + fontMetrics.bottom + 30, paint);
        } else if ((this.width * (1 - valueX)) < widthText / 2) {
            canvas.drawText(text, this.width - widthText / 2, startHeight + bitmap.getHeight() + KEY_HEIGHT + fontMetrics.bottom + 30, paint);
        } else {
            canvas.drawText(text, ((int) (this.width * valueX)), startHeight + bitmap.getHeight() + KEY_HEIGHT + fontMetrics.bottom + 30, paint);
        }
    }

    public void setProgress(float valueX) {
        this.valueX = valueX;
        invalidate();
    }
}
